class RegionsController < ApplicationController
   before_filter :login_required, :only => [:new, :create]
   require_role "superadmin", :for => [:create, :new]
   
  def index
  end
  
  def show
    @region = Region.find(params[:id])
    @desc_languages = @region.region_descriptions
    @region_art = {
      :random => @region.get_artworks(:random),
      :new => @region.get_artworks(:new),
    }
  end
  
  def new
    @region = Region.new
    respond_to do |format|
      format.html { render :action => "new", :layout => "layouts/superadmin"}
      format.xml  { render :xml => @region }
    end
  end
  
  def create
    @region = Region.new(params[:region])
    
    respond_to do |format|
      if @region.save
        flash[:notice] = 'Region was successfully created.'
        format.html { redirect_to :action => 'region_list' }
        format.xml  { render :xml => @region, :status => :created, :location => @region }
      else
        format.html { render :action => "region_add" }
        format.xml  { render :xml => @region.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  def search
  end
  
  def add_to_user
    @region_for = User.find(params[:user])
    
    if params[:region] == "-- Add Region --" then
      render :partial => "admined", :layout => false
      return
    end
    new_region = Region.find(params[:region])
    @region_for.add_region(new_region)
    
    if !@region_for.has_role?("admin")
      @region_for.add_role(Role.find_by_name("admin"))
    end
    
    render :partial => "admined", :layout => false
  end
  
  def remove_from_user
    @region_for = User.find(params[:user])
    region = Region.find(params[:region])
    
    @region_for.remove_region(region)
    if @region_for.roles.collect.length < 1
      @region_for.remove_role(Role.find_by_name("admin"))
    end
    render :partial => "admined", :layout => false
  end
end
